<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/31
 * Time: 14:53
 */

namespace app\middleware;

use think\Validate;

class ValidateData extends Validate
{
    /**https://www.kancloud.cn/xieyongfa123/thinkphp_note/822627
     * 默认返回资源类型
     * @var \think\Request $request
     * @var mixed $next
     * @var string $name
     * @throws \Exception
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        //获取当前参数

        $controller = $request->controller();
        $module = app('http')->getName();
        $action = $request->action();
        $validate = "app\\validate\\" . $controller;

        if (class_exists($validate)) {
            try {
                $has_scene =validate($validate)->hasScene($action);//要不没有场景会走默认全部场景

                if($has_scene){
                    $result = validate($validate)->scene($action)->check($request->data);
                }

            } catch (\Exception $e) {
                abort(response(['code' => 401, 'msg' => $e->getMessage()]));
            }

        }
        return $next($request);
    }

    public function end(\think\Response $response)
    {

    }
}